﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using com.tiyma.model;
using System.Collections.Generic;
using System.IO;
using Lucene.Net.Search;
using Lucene.Net.QueryParsers;
using Lucene.Net.Documents;

namespace com.tiyma.iremoting
{
    /// <summary>
    /// IRemoting 的摘要说明
    /// </summary>
    [WebService(Namespace = "com.tiyma.iremoting")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class IRemoting : System.Web.Services.WebService
    {
        #region 搜索小说
        /// <summary>
        /// 搜索小说
        /// </summary>
        /// <param name="i_key_word"></param>
        /// <param name="i_search_type">1、书名 2、作者 3、分类 4、综合</param>
        /// <param name="i_page_index"></param>
        /// <param name="i_page_size"></param>
        /// <param name="o_total_count"></param>
        /// <returns></returns>
        [WebMethod]
        public List<BookInfo> SearchBook(string i_key_word, int i_search_type, int i_page_index, int i_page_size, out int o_total_count)
        {
            i_key_word = "星辰变";
            o_total_count = 0;
            string t_search_field = "";

            if (i_search_type ==2)
                t_search_field = "AUTHORNAME";

            if (i_search_type == 1)
                t_search_field = "BOOKNAME";

            if (i_search_type == 3)
                t_search_field = "BOOKCATEGORY";
            IndexSearcher t_seo = null;
            try
            {
                string t_index_path =AppUtil.BookIndexPath;

                if (string.IsNullOrEmpty(t_index_path) || !Directory.Exists(t_index_path))
                {
                    AppUtil.Info("搜索小说的目录不存在!" + t_index_path);
                    return null;
                }

                BooleanQuery t_bq = new BooleanQuery();
                if (!string.IsNullOrEmpty(t_search_field))
                {
                    QueryParser t_query_parse = new QueryParser(t_search_field, new Lucene.Net.Analysis.Standard.StandardAnalyzer());

                    Query t_query = t_query_parse.Parse(i_key_word.Trim());
                    t_bq.Add(t_query, BooleanClause.Occur.MUST);
                }
                else
                {
                    QueryParser t_book_name_query = new QueryParser("BOOKNAME", new Lucene.Net.Analysis.Standard.StandardAnalyzer());
                    Query t_query_1 = t_book_name_query.Parse(i_key_word);
                    t_bq.Add(t_query_1, BooleanClause.Occur.SHOULD);

                    QueryParser t_autuor_name_query = new QueryParser("AUTHORNAME", new Lucene.Net.Analysis.Standard.StandardAnalyzer());
                    Query t_query_2 = t_autuor_name_query.Parse(i_key_word);
                    t_bq.Add(t_query_2, BooleanClause.Occur.SHOULD);

                    QueryParser t_category_query = new QueryParser("BOOKCATEGORY", new Lucene.Net.Analysis.Standard.StandardAnalyzer());
                    Query t_query_3 = t_category_query.Parse(i_key_word);
                    t_bq.Add(t_query_3, BooleanClause.Occur.SHOULD);

                    QueryParser t_sub_category_query = new QueryParser("BOOKSUBCATEGORYNAME", new Lucene.Net.Analysis.Standard.StandardAnalyzer());
                    Query t_query_4 = t_sub_category_query.Parse(i_key_word);
                    t_bq.Add(t_query_3, BooleanClause.Occur.SHOULD);
                }

                Sort t_sort = new Sort();
                SortField t_sort_field = new SortField("BOOKCHAPTERCOUNT", SortField.INT, true);//按照BOOKCHAPTERCOUNT字段排序，true表示降序
                t_sort.SetSort(t_sort_field);


                t_seo = new IndexSearcher(AppUtil.BookIndexPath);

                Hits t_hits = t_seo.Search(t_bq, t_sort);

                o_total_count = t_hits.Length();

                if (o_total_count == 0)
                {
                    AppUtil.Info(string.Format("从seo路径：{0}搜索关键字：{1}得到0条记录", AppUtil.BookIndexPath, i_key_word));
                }

                List<BookInfo> t_list = new List<BookInfo>();

                int t_site_id = 1;

                BookInfo t_book = null;

                for (int index = (i_page_index - 1) * i_page_size; index < o_total_count && index < (i_page_index * i_page_size + 1); index++)
                {
                    Document t_doc = t_hits.Doc(index);

                    t_book = new BookInfo();

                    t_book.BookId = t_doc.Get("BOOKID");
                    t_book.BookName = t_doc.Get("BOOKNAME");
                    t_book.AuthorName = t_doc.Get("AUTHORNAME");
                    t_book.BookCategory = t_doc.Get("BOOKCATEGORY");
                    t_book.BookSubCategory = t_doc.Get("BOOKSUBCATEGORYNAME");
                    t_book.Description = t_doc.Get("DESCRIPTION");
                    t_book.LastUpdateChapterId = t_doc.Get("LASTUPDATECHAPTERID");
                    t_book.LastUpdateChapterName = t_doc.Get("LASTUPDATECHAPTERNAME");
                    t_book.LastUpdateTime = DateTime.Parse(t_doc.Get("LASTUPDATETIME"));
                    t_book.BookChapterCount = string.IsNullOrEmpty(t_doc.Get("BOOKCHAPTERCOUNT")) ? 0 : Convert.ToInt32(t_doc.Get("BOOKCHAPTERCOUNT"));
                    if (!int.TryParse(t_doc.Get("SITEID"), out t_site_id))
                        t_site_id = 1;
                    t_book.SiteId = t_site_id;
                    t_book.SiteName = t_doc.Get("SITENAME");
                    t_list.Add(t_book);
                }

                t_seo.Close();
                return t_list;


            }
            catch (Exception ex)
            {
                AppUtil.Error("搜索小说失败" + ex.ToString());
                if (t_seo != null)
                    t_seo.Close();
                return null;
            }
        }

        #endregion


        #region 获取你可能喜欢的小说
        /// <summary>
        /// 获取推荐的你可能喜欢看得小说
        /// </summary>
        /// <param name="i_book_name"></param>
        /// <param name="i_category_name"></param>
        /// <param name="i_author_name"></param>
        /// <returns></returns>
        [WebMethod]
        public List<BookInfo> GetRecommendBook(string i_book_name, string i_category_name, string i_author_name)
        {
            int t=0;
            SearchBook("", 1, 1, 10, out t);
            List<BookInfo> t_list = null;
            IndexSearcher t_seo = null;
            try
            {
                string t_index_path = AppUtil.BookIndexPath;

                if (string.IsNullOrEmpty(t_index_path) || !Directory.Exists(t_index_path))
                {
                    AppUtil.Info("搜索小说的目录不存在!" + t_index_path);
                    return null;
                }

                BooleanQuery t_bq = new BooleanQuery();

                QueryParser t_book_name_query = new QueryParser("BOOKNAME", new Lucene.Net.Analysis.Standard.StandardAnalyzer());
                Query t_query_1 = t_book_name_query.Parse(i_book_name);
                t_bq.Add(t_query_1, BooleanClause.Occur.MUST_NOT);

                if (!string.IsNullOrEmpty(i_author_name))
                {
                    QueryParser t_autuor_name_query = new QueryParser("AUTHORNAME", new Lucene.Net.Analysis.Standard.StandardAnalyzer());
                    Query t_query_2 = t_autuor_name_query.Parse(i_author_name);
                    t_bq.Add(t_query_2, BooleanClause.Occur.SHOULD);
                }

                if (!string.IsNullOrEmpty(i_category_name))
                {
                    QueryParser t_category_query = new QueryParser("BOOKCATEGORY", new Lucene.Net.Analysis.Standard.StandardAnalyzer());
                    Query t_query_3 = t_category_query.Parse(i_category_name);
                    t_bq.Add(t_query_3, BooleanClause.Occur.SHOULD);
                }

                t_seo = new IndexSearcher(t_index_path);

                Hits t_hits = t_seo.Search(t_bq);

                if (t_hits.Length() == 0)
                {
                    AppUtil.Info(string.Format("从seo路径：{0}搜索关键字：{1}得到0条记录", AppUtil.BookIndexPath, i_book_name));
                }

                t_list = new List<BookInfo>();
                BookInfo t_book = null;
                int t_site_id = 0;

                Dictionary<string, string> t_dic = new Dictionary<string, string>();
                for (int index = 0; index < t_hits.Length(); index++)
                {

                    Document t_doc = t_hits.Doc(index);
                    t_book = new BookInfo();
                    t_book.BookId = t_doc.Get("BOOKID");
                    t_book.BookName = t_doc.Get("BOOKNAME");
                    t_book.AuthorName = t_doc.Get("AUTHORNAME");
                    t_book.BookCategory = t_doc.Get("BOOKCATEGORY");
                    t_book.LastUpdateChapterName = t_doc.Get("LASTUPDATECHAPTERNAME");

                    if (!int.TryParse(t_doc.Get("SITEID"), out t_site_id))
                        t_site_id = 1;
                    t_book.SiteId = t_site_id;
                    t_book.SiteName = t_doc.Get("SITENAME");

                    if (!t_dic.ContainsKey(t_book.BookName))
                    {
                        t_dic.Add(t_book.BookName, t_book.BookName);
                        t_list.Add(t_book);
                    }

                    if (t_dic.Count == 10)
                        break;
                }
                t_seo.Close();
                return t_list;
            }
            catch (Exception ex)
            {
                AppUtil.Error("获取你可能喜欢看的小说失败" + ex.ToString());
                if (t_seo != null)
                    t_seo.Close();
                return null;
            }
        }
        #endregion

        #region 根据图书名称获取图书信息
        /// <summary>
        /// 根据图书名称列表获取小说信息
        /// </summary>
        /// <param name="i_list"></param>
        /// <returns></returns>
        [WebMethod]
        public List<BookInfo> GetBookListByBookNames(List<string> i_list)
        {
            if (i_list == null)
                return null;

            IndexSearcher t_seo = null;
            try
            {

                List<BookInfo> t_list = null;

                string t_index_path =AppUtil.BookIndexPath;

                if (string.IsNullOrEmpty(t_index_path) || !Directory.Exists(t_index_path))
                {
                    AppUtil.Info("搜索小说的目录不存在!");
                    return null;
                }

                BooleanQuery t_bq = new BooleanQuery();
                foreach (string t_book_name in i_list)
                {
                    QueryParser t_book_name_query = new QueryParser("BOOKNAME", new Lucene.Net.Analysis.Standard.StandardAnalyzer());
                    Query t_query = t_book_name_query.Parse(t_book_name);
                    t_bq.Add(t_query, BooleanClause.Occur.SHOULD);
                }

                t_seo = new IndexSearcher(AppUtil.BookIndexPath);

                Hits t_hits = t_seo.Search(t_bq);

                if (t_hits.Length() == 0)
                {
                    AppUtil.Info("lucune获取最近读者书架图书失败");
                }

                t_list = new List<BookInfo>();
                BookInfo t_book = null;
                int t_site_id = 0;

                Dictionary<string, string> t_dic = new Dictionary<string, string>();
                for (int index = 0; index < t_hits.Length(); index++)
                {

                    Document t_doc = t_hits.Doc(index);
                    t_book = new BookInfo();
                    t_book.BookId = t_doc.Get("BOOKID");
                    t_book.BookName = t_doc.Get("BOOKNAME");
                    t_book.AuthorName = t_doc.Get("AUTHORNAME");
                    t_book.BookCategory = t_doc.Get("BOOKCATEGORY");
                    t_book.BookSubCategory = t_doc.Get("BOOKSUBCATEGORYNAME");
                    t_book.Description = t_doc.Get("DESCRIPTION");
                    t_book.LastUpdateChapterId = t_doc.Get("LASTUPDATECHAPTERID");
                    t_book.LastUpdateChapterName = t_doc.Get("LASTUPDATECHAPTERNAME");
                    t_book.LastUpdateTime = DateTime.Parse(t_doc.Get("LASTUPDATETIME"));

                    if (!int.TryParse(t_doc.Get("SITEID"), out t_site_id))
                        t_site_id = 1;
                    t_book.SiteId = t_site_id;
                    t_book.SiteName = t_doc.Get("SITENAME");

                    if (!t_dic.ContainsKey(t_book.BookName))
                    {
                        t_dic.Add(t_book.BookName, t_book.BookName);
                        t_list.Add(t_book);
                    }
                }
                t_seo.Close();
                return t_list;
            }
            catch (Exception ex)
            {
                AppUtil.Error("获取用户书架图书失败" + ex.ToString());
                if (t_seo != null)
                    t_seo.Close();
                return null;
            }
        }
        #endregion


    }
}
